package com.kitchen.ui.dialog.storage
{
	import com.kitchen.AbstractProxy;
	import com.kitchen.http.KitchenProxy;
	import com.kitchen.http.Proxy;
	import com.kitchen.ui.com.PiercedNumber;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class StorageGrid extends Sprite
	{
		private var dialogSkin:BitmapData;
		private var mainSkin:BitmapData;
		private var cellArray:Array;
		private var pageArray:Array;
		public var rows:int;
		public var cols:int;
		public var page:int;
		private var container:Sprite;
		private var nextPage:int;
		private var tox:int;
		private var timer:Timer;
		public var w:int;
		public var h:int;
		public var state:int;
		private var kitProxy:KitchenProxy = null;
		
		public function StorageGrid(mainSkin:BitmapData, dialogSkin:BitmapData, cols:int, rows:int)
		{
			super();
			this.dialogSkin = dialogSkin;
			this.mainSkin = mainSkin;
			this.rows = rows;
			this.cols = cols;
			this.nextPage = 0;
			this.page = 0;
			this.tox = 0;
			this.state = 0;
			this.cellArray = new Array();
			this.pageArray = new Array();
			this.timer = new Timer(25, 0);
			this.timer.addEventListener(TimerEvent.TIMER, timerHandler);
			this.layout();
		}
		
		private function layout():void{
			this.w = this.cols*51+10;
			this.h = this.rows*51+10;
			var bg:Shape = new Shape();
			bg.graphics.lineStyle(1, 0xd1b36d);
			bg.graphics.beginFill(0xfcfcf2, 1);
			bg.graphics.drawRoundRect(-5, -5, this.w, this.h+20, 15, 15);
			bg.graphics.endFill();
			this.addChild(bg);
			this.container = new Sprite();
			this.addChild(this.container);
			var mask:Shape = new Shape();
			mask.graphics.beginFill(0xffffff, 1);
			mask.graphics.drawRoundRect(-6, -6, this.w+2, this.h+22, 15, 15);
			mask.graphics.endFill();
			this.addChild(mask);
			this.mask = mask;
			this.goPage(1, 1);
		}
		
		public function goPage(p:int, st:int=1):void{
			if((st == this.state) && (this.page == p)){
				return;
			}
			this.state = st;
			this.nextPage = p;
			if(this.state == 1){
				this.getProxy().getSysProp(p);
			}else if(this.state == 2){
				this.getProxy().getSysProp(p);
			}
		}
		
		private function render(result:Object):void{
			var cx:int = (this.nextPage-1)*this.cols*51;
			this.tox = -cx;
			var max:int = this.rows*this.cols;
			var tmpArr:Array = new Array();
			var data:Array = result.data;
			for(var i:int = 0; i<max; i++){
				var cell:StorageGridCell = null;
				if(data[i] != null){
					cell = new StorageGridCell(this.dialogSkin, data[i].id, data[i]);
					cell.addEventListener(MouseEvent.CLICK, propClickHandler);
				}else{
					cell = new StorageGridCell(this.dialogSkin, 0);
				}
				cell.x = cx+(i%this.cols)*51;
				cell.y = Math.floor(i/this.cols)*51;
				tmpArr.push(cell);
				this.container.addChild(cell);
			}
			this.cellArray[this.nextPage] = tmpArr;
			this.pageItem(this.nextPage, result.pager.ptotal);
			this.timer.start();
		}
		
		//数据返回
		private function renderHandler(event:Event):void{
			var result:Object = AbstractProxy(event.currentTarget).result;
			if(result.code == 200){
				this.render(result.result);
			}else{
				trace('数据获取失败。。');
			}
		}
		
		private function pageItem(page:int, ptotal:int):void{
			var pages:String ='';
			var i:int = 1;
			if(8 > ptotal){
				for(;i<=ptotal;i++){
					pages += i+'';
				}
			}else if(page < 5){
				pages = '1234567';
			}else if(page>=(ptotal-4)){
				i = ptotal -6;
				for(;i<=ptotal; i++){
					pages += i+'';
				}
			}
			this.showPage(pages, page);
		}
		
		
		private function showPage(pages:String, page:int):void{
			var max:int = pages.length > this.pageArray.length?pages.length:this.pageArray.length;
			for(var i:int=0; i<max; i++){
				var n:int = int(pages.substr(-(i+1), 1));
				if(0 < n){
					if(this.pageArray[i] == null){
						var pn:PiercedNumber = new PiercedNumber(this.mainSkin);
						pn.x = this.w-30-(i*22);
						pn.y = this.h-10;
						pn.addEventListener(MouseEvent.CLICK, numberClickHandle);
						this.addChild(pn);
						this.pageArray[i] = pn;
					}
					if(n == page){
						PiercedNumber(this.pageArray[i]).setChecked(true);
					}else{
						PiercedNumber(this.pageArray[i]).setChecked(false);
					}
					PiercedNumber(this.pageArray[i]).show(n);
				}else{
					if(this.pageArray[i] != null){
						this.pageArray[i].visible = false;
					}
				}
			}
		}
		
		private function propClickHandler(me:MouseEvent):void{
			if(!StorageGridCell(me.currentTarget).checked){
				StorageGridCell(me.currentTarget).setChecked(true);
			}else{
				StorageGridCell(me.currentTarget).setChecked(false);
			}
		}
		
		private function numberClickHandle(me:MouseEvent):void{
			var num:int = me.target.num;
			this.goPage(num);
		}
		
		//
		private function timerHandler(event:TimerEvent):void{
			if(this.container.x != this.tox){
				if(Math.abs(this.container.x - this.tox) > 10){
					this.container.x = Math.floor((this.container.x+this.tox)/2);
				}else{
					this.container.x = this.tox;
				}
			}else{
				this.timer.stop();
				if(this.cellArray[this.page] != null){
					for each(var item:StorageGridCell in this.cellArray[this.page]){
						this.container.removeChild(item);
					}
				}
				this.cellArray[this.page] = null;
				this.page = this.nextPage;
				this.nextPage = 0;
			}
		}
		
		private function getProxy():KitchenProxy{
			if(this.kitProxy == null){
				this.kitProxy = KitchenProxy.getInstance();
				this.kitProxy.addEventListener(AbstractProxy.E_OK, renderHandler);
			}
			return this.kitProxy;
		}
	}
}